package com.csi.service.impl;

import com.csi.dao.EmployeeDao;
import com.csi.dao.UserDao;
import com.csi.dao.impl.EmployeeDaoImpl;
import com.csi.dao.impl.UserDaoImpl;
import com.csi.domain.User;
import com.csi.pojo.PageBean;
import com.csi.domain.Employee;
import com.csi.service.EmployeeService;

import java.sql.SQLException;
import java.util.List;

public class EmployeeServiceImpl implements EmployeeService {
    @Override
    public PageBean<Employee> findPage(int currentPageNumber) {

        EmployeeDao employeeDao = new EmployeeDaoImpl();

        PageBean<Employee> page = null;
        try {
            page = employeeDao.findPage(currentPageNumber);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return page;
    }

    @Override
    public void add(User user, Employee employee) {

        EmployeeDao employeeDao = new EmployeeDaoImpl();

        String lastEmployeeId = null; // 最后的员工编号
        try {
            lastEmployeeId = employeeDao.findLast();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        String substring = lastEmployeeId.substring(1, 7);
        int i = Integer.valueOf(substring) + 1;
        employee.setEmployeeAccount("E" + i);

        String lastUserId = null; // 用户最后的用户编号
        UserDao userDao = new UserDaoImpl();

        try {
            lastUserId = userDao.findLast();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        long j = Long.valueOf(lastUserId.substring(1, 12)) + 1;
        user.setUserNumber("U" + j);

        try {
            employeeDao.add(user,employee);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Employee> findAll() {

        EmployeeDao employeeDao = new EmployeeDaoImpl();

        List<Employee> list = null;
        try {
            list = employeeDao.findAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public Employee findById(Integer userId) {
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        Employee employee = null;
        try {
            employee = employeeDao.findById(userId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return employee;
    }

    @Override
    public void updateRole(Integer employeeId,Integer userId, List<Integer> roles) {
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        try {
            employeeDao.updateRole(employeeId,userId,roles);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Employee findByLoginAccount(String loginAccount) {
        EmployeeDao employeeDao = new EmployeeDaoImpl();

        Employee employee = null;
        try {
            employee = employeeDao.findByLoginAccount(loginAccount);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return employee;
    }
}
